package com.example.util;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.security.MessageDigest;
import java.util.Random;





public class SecurityUtils {
	private static final String CHARS = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
	
	private static final int WIDTH = 120;
	
	private static final int HEIGHT = 40;
//	/**
//	 * @author 黄晓峰
//	 * @Description: 生成默认的验证码图片
//	 * @CreateDate 2014-11-8 下午10:51:05
//	 * @param code
//	 * @return BufferedImage
//	 */
//	public static ValidCode createValidCode() {
//		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
//		Graphics graphic = image.getGraphics();
//		char[] codes = getCode(4);
//		drawBackground(graphic);
//		drawRands(graphic, codes);
//		graphic.dispose();
//		return new ValidCode(toStr(codes), image);
//	}
	/**
	 * 产生随机数
	 */
	private static char[] getCode(int length) {
		char[] rands = new char[length];
		for (int i = 0; i < length; i++) {
			int rand = (int) (Math.random() * CHARS.length());
			rands[i] = CHARS.charAt(rand);
		}
		return rands;
	}
	
	/**
	 * 绘制背景
	 */
	private static void drawBackground(Graphics g) {
		g.setColor(new Color(255,255,255));
		g.fillRect(0, 0, WIDTH, HEIGHT);
		Random random = new Random();
		int len = 0;
		while (len <= 5) {
			len = random.nextInt(15);
		}
		for (int i = 0; i < len; i++) {
			int x = (int) (random.nextInt(WIDTH));
			int y = (int) (random.nextInt(HEIGHT));
			int red = (int) (255 - random.nextInt(200));
			int green = (int) (255 - random.nextInt(200));
			int blue = (int) (255 - random.nextInt(200));
			g.setColor(new Color(red, green, blue));
			// g.drawLine(x, y, random.nextInt(WIDTH)-x,
			// random.nextInt(HEIGHT)-y);
			g.drawOval(x, y, 2, 2);
		}
	}

	/**
	 * 绘制验证码
	 */
	private static void drawRands(Graphics g, char[] rands) {
		Random random = new Random();
		//字体大小为图片高度的70%
		int fsize = (int)(HEIGHT * 0.7);
		g.setFont(new Font(Font.DIALOG, Font.BOLD, fsize));
		for (int i = 0; i < rands.length; i++) {
			int red = (int) (random.nextInt(255));
			int green = (int) (random.nextInt(255));
			int blue = (int) (random.nextInt(255));
			g.setColor(new Color(red, green, blue));
			g.drawString("" + rands[i], i * 30, 30);
		}
	}
	
//	/**
//	 * @author 黄晓峰
//	 * @Description: 将char数组转换成字符串，例如['a', '1'] -> "a1"
//	 * @Datetime 2015-5-1
//	 * @param chars
//	 * @return String
//	 */
//	public static String toStr( char[] chars ) {
//		if ( ArrayUtils.isEmpty(chars) ) {
//			return "";
//		} else {
//			StringBuffer buffer = new StringBuffer();
//			return buffer.append(chars).toString();
//		}
//	}
	
	public static String string2MD5(String inStr){  
        MessageDigest md5 = null;  
        try{  
            md5 = MessageDigest.getInstance("MD5");  
        }catch (Exception e){  
            System.out.println(e.toString());  
            e.printStackTrace();  
            return "";  
        }  
        char[] charArray = inStr.toCharArray();  
        byte[] byteArray = new byte[charArray.length];  
  
        for (int i = 0; i < charArray.length; i++)  
            byteArray[i] = (byte) charArray[i];  
        byte[] md5Bytes = md5.digest(byteArray);  
        StringBuffer hexValue = new StringBuffer();  
        for (int i = 0; i < md5Bytes.length; i++){  
            int val = ((int) md5Bytes[i]) & 0xff;  
            if (val < 16)  
                hexValue.append("0");  
            hexValue.append(Integer.toHexString(val));  
        }  
        return hexValue.toString();  
  
    }  
  
    /** 
     * 加密解密算法 执行一次加密，两次解密 
     */   
    public static String convertMD5(String inStr){  
    	byte[] a = inStr.getBytes();
//        char[] a = inStr.toCharArray();  
        for (int i = 0; i < a.length; i++){  
            a[i] = (byte) (a[i] ^ 't');  
        }  
        String s = new String(a);  
        return s;  
  
    }  
}
